<%@ page contentType="text/html; charset=utf-8"%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<c:set var="basePath" value="${pageContext.request.contextPath}"/>
<%--<c:set var="upmsAgents" value="${upmsAgents}"/>--%>

<div id="createDialog" class="container-fluid">
    <div class="row">
        <form id="createForm" method="post" class="form-horizontal" action="">
            <div class="form-group">
                <label class="col-sm-4 control-label" for="name">
                    <span class="help-inline"><font color="red">*</font> </span>
                    名称：</label>
                <div class="col-sm-5">
                    <input type="text" class="form-control" id="name" name="name" placeholder="名称" />
                </div>
            </div>
            <%--<div class="form-group">--%>
                <%--<label class="col-sm-4 control-label" for="agent" >--%>
                    <%--<span class="help-inline"><font color="red">*</font> </span>--%>
                    <%--上级代理：</label>--%>
                <%--<div class="col-sm-5">--%>
                    <%--<div id="agent" data-id="${pagent.id}" data-name="${pagent.name}" ></div>--%>
                <%--</div>--%>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="agent" >
                    <span class="help-inline"><font color="red">*</font> </span>
                    上级代理：</label>
                <div class="col-sm-5">
                    <div id="agent" data-id="${pagent.id}" data-name="${pagent.name}" ></div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="agenttype">
                    <span class="help-inline"><font color="red">*</font> </span>
                    代理类型：</label>
                <div class="col-sm-5">
                    <select id="agenttype" name="agenttype" class="form-control">
                        <c:forEach var="upmsDict" items="${agentTypeList}">
                            <option value="${upmsDict.dicValue}">${upmsDict.dicText}</option>
                        </c:forEach>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="rate">
                    <span class="help-inline"><font color="red">*</font> </span>
                    营业额扣除百分比(%)：</label>
                <div class="col-sm-5">
                    <input type="text" class="form-control" id="rate" name="rate" placeholder="营业额扣除百分比(%)" />
                </div>
            </div>
            <%--<div class="form-group">--%>
                <%--<label class="col-sm-4 control-label" for="agenttype">--%>
                    <%--<span class="help-inline"><font color="red">*</font> </span>--%>
                    <%--代理类型：</label>--%>
                <%--<div class="col-sm-5">--%>
                    <%--<div id="input_ztree" data-id="${pagent.id}" data-name="${pagent.name}" ></div>--%>
                    <%--&lt;%&ndash;<select id="input_ztree"  readonly="true" name="input_ztree" class="form-control"></select>&ndash;%&gt;--%>
                    <%--&lt;%&ndash;<input type="hidden" id="autoPartition" name="autoPartition">&ndash;%&gt;--%>
                <%--</div>--%>
            <%--</div>--%>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="usethirdapp">
                    <span class="help-inline"><font color="red">*</font> </span>
                    是否使用独立小程序：</label>
                <div class="col-sm-5">
                    <select id="usethirdapp" name="usethirdapp" class="form-control">
                        <c:forEach var="upmsDict" items="${yesNoList}">
                            <option value="${upmsDict.dicValue}" <c:if test="${upmsDict.dicValue==0}">selected</c:if>>${upmsDict.dicText}</option>
                        </c:forEach>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="description">描述：</label>
                <div class="col-sm-5">
                    <textarea class="form-control" id="description" name="description" placeholder="描述"></textarea>
                </div>
            </div>
            <div class="form-group">
                <div class="col-sm-8 col-sm-offset-4">
                    <button type="button" class="btn btn-primary" onclick="createSubmit();">
                        保存
                    </button>
                    <button type="button" class="btn btn-default"
                            onclick="createDialog.close();">取消
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>
<script>
    function validateForm(){
        return $( "#createForm" ).validate( {
            ignore: ["pid"],
            rules: {
                pid: "required",
                name: {
                    required: true,
                    maxlength: 64
                },
                description: {
                    maxlength: 500
                },
                rate:"required"
            },
            messages: {
                rate: "该项是必填项。",
                name: "名称是必填项。",
                pid: "上级代理是必填项。",
                name: {
                    required: "名称是必填项。",
                    maxlength: "名称长度超出64个字符。"
                },
                description: {
                    maxlength: "描述长度超出500个字符"
                }
            },
            errorElement: "em",
            errorPlacement: function ( error, element ) {
                // Add the `help-block` class to the error element
                error.addClass( "help-block" );

                if ( element.prop( "type" ) === "checkbox" ) {
                    error.insertAfter( element.parent( "label" ) );
                } else {
                    error.insertAfter( element );
                }
            },
            highlight: function ( element, errorClass, validClass ) {
                $( element ).parents( ".col-sm-5" ).addClass( "has-error" ).removeClass( "has-success" );
            },
            unhighlight: function (element, errorClass, validClass) {
                $( element ).parents( ".col-sm-5" ).addClass( "has-success" ).removeClass( "has-error" );
            }
        } );
    }
    //注册表单验证
    $(validateForm());

    $(function(){
        //初始化树形select控件
        <%--$("#agent").bootstrapCombotree({--%>
            <%--data: ${upmsAgents},//data应符合实例的data格式--%>
            <%--defaultLable:"请选择",--%>
            <%--name : 'pid'//combotree值域的name，可以用在表单提交--%>
        <%--});--%>
        $("#agent").ztreeSelect({
            name:"pid",
            url: '${basePath}/manage/agentTree',
            params: {isLeaf: 1},
            renderId: "agent",
            callback: function (nodeIds, treeNode) {
            }
        })
    });

    function createSubmit() {
        if(validateForm().form()){
            $.ajax({dataType: 'json',
                type: 'post',
                url: '${basePath}/manage/agent/create',
                data: $('#createForm').serialize(),
                success: function(result) {
                    if (result.code != 1) {
                        if (result.data instanceof Array) {
                            $.each(result.data, function(index, value) {
                                infoDialog(value.errorMsg);
                            });
                        } else {
                            infoDialog(result.data);
                        }
                    } else {
                        createDialog.close();
                        $table.bootstrapTable('refresh');
                    }
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    infoDialog(textStatus);
                }
            });
        }
    }
</script>